home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / source_code / dhtmlunl / dhtml.exe / CD Content / Chap12 / dun12_8.txt < prev    next >
Encoding:
Text File  |  1997-12-18  |  2.2 KB  |  105 lines

  1. <HTML>
  2.  
  3. <HEAD>
  4.  
  5. <TITLE>moving layer object</TITLE>
  6.  
  7. </HEAD>
  8.  
  9.  
  10.  
  11. <SCRIPT LANGUAGE="JavaScript">
  12.  
  13. var L=new layerTool();
  14.  
  15. function layerTool()
  16.  
  17.     {
  18.  
  19.     if (navigator.appName=="Netscape")
  20.  
  21.         this.layerProp=navProp;
  22.  
  23.     else
  24.  
  25.         this.layerProp=exProp;
  26.  
  27.     }
  28.  
  29. function exProp()
  30.  
  31.     {
  32.  
  33.     return document.all[arguments[arguments.length-1]].style;
  34.  
  35.     }
  36.  
  37. function navProp()
  38.  
  39.     {
  40.  
  41.     retVal="";
  42.  
  43.     for (var x=0;x<arguments.length;x++)
  44.  
  45.         {
  46.  
  47.         retVal+="document.layers[\'"+arguments[x]+"\']";
  48.  
  49.         if (x!=arguments.length-1)
  50.  
  51.             retVal+=".";
  52.  
  53.         }
  54.  
  55.     return eval(retVal);
  56.  
  57.     }
  58.  
  59. function Point(X,Y)
  60.  
  61.     {
  62.  
  63.     this.x=X;
  64.  
  65.     this.y=Y;
  66.  
  67.     }
  68.  
  69. function layerObject(layerID,pos,vel,z)
  70.  
  71.     {
  72.  
  73.     // properties
  74.  
  75.     this.layerID=layerID;
  76.  
  77.     this.position=pos;
  78.  
  79.     this.velocity=vel;
  80.  
  81.     this.depth=z;
  82.  
  83.     this.visibility="visible";
  84.  
  85.     // methods
  86.  
  87.     this.show=showLayer;
  88.  
  89.     this.hide=hideLayer;
  90.  
  91.     this.setPosition=setPosition;
  92.  
  93.     this.draw=drawLayer;
  94.  
  95.     this.update=layerUpdate;
  96.  
  97.     this.show();
  98.  
  99.     }
  100.  
  101. function setZorder(z)
  102.  
  103.     {
  104.  
  105.     this.depth=z;
  106.  
  107.     }
  108.  
  109. function showLayer()
  110.  
  111.     {
  112.  
  113.     L.layerProp(this.layerID).visibility="visible";
  114.  
  115.     }
  116.  
  117. function hideLayer()
  118.  
  119.     {
  120.  
  121.     L.layerProp(this.layerID).visibility="hidden";
  122.  
  123.     }
  124.  
  125. function drawLayer()
  126.  
  127.     {
  128.  
  129.     L.layerProp(this.layerID).zIndex=this.depth;
  130.  
  131.     L.layerProp(this.layerID).left=(this.position).x;
  132.  
  133.     L.layerProp(this.layerID).top=(this.position).y;
  134.  
  135.     }
  136.  
  137. function setPosition(pos)
  138.  
  139.     {
  140.  
  141.     this.position=pos;
  142.  
  143.     }
  144.  
  145. function layerUpdate()
  146.  
  147.     {
  148.  
  149.     var newPos=new Point(this.position.x+this.velocity.x,
  150.  
  151.                         this.position.y+this.velocity.y);
  152.  
  153.     this.setPosition(newPos);
  154.  
  155.     }
  156.  
  157. function init()
  158.  
  159.     {
  160.  
  161.     mySprite=new layerObject("sprite1",new Point(0,0),new Point(5,5),1);
  162.  
  163.     cycle();
  164.  
  165.     }
  166.  
  167. function cycle()
  168.  
  169.     {
  170.  
  171.     mySprite.update();
  172.  
  173.     mySprite.draw();
  174.  
  175.     setTimeout("cycle()",30);
  176.  
  177.     }
  178.  
  179. </SCRIPT>
  180.  
  181. <STYLE>
  182.  
  183.  
  184.  
  185. #sprite1{
  186.  
  187.     POSITION: absolute;
  188.  
  189.     VISIBILITY: hidden
  190.  
  191.     }
  192.  
  193.  
  194.  
  195. </STYLE>
  196.  
  197. <BODY BGCOLOR="#000000" onLoad="init()">
  198.  
  199. <DIV ID="sprite1">
  200.  
  201. <IMG SRC="rock.gif">
  202.  
  203. </DIV>
  204.  
  205. </BODY>
  206.  
  207. </HTML>
  208.  
  209.